package com.muyan.sys.webscoket.controller;

import com.muyan.sys.webscoket.entity.ChatGroup;
import com.muyan.sys.webscoket.service.ChatGroupService;
import com.muyan.sys.webscoket.service.ChatMessageService;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Set;

@RestController
@RequestMapping("/api/chat")
public class ChatController {
    
    private final ChatGroupService chatGroupService;
    private final ChatMessageService chatMessageService;
    
    public ChatController(ChatGroupService chatGroupService, 
                         ChatMessageService chatMessageService) {
        this.chatGroupService = chatGroupService;
        this.chatMessageService = chatMessageService;
    }
    
    /**
     * 创建群组
     */
    @PostMapping("/group")
    public ChatGroup createGroup(@RequestParam String groupName,
                                 @RequestParam String creatorId,
                                 @RequestParam String description) {
        return chatGroupService.createGroup(groupName, creatorId, description,new Date());
    }
    
    /**
     * 添加群成员
     */
    @PostMapping("/group/{groupId}/member/{userId}")
    public void addGroupMember(@PathVariable String groupId, @PathVariable String userId) {
        chatGroupService.addGroupMember(groupId, userId);
    }
    
    /**
     * 移除群成员
     */
    @DeleteMapping("/group/{groupId}/member/{userId}")
    public void removeGroupMember(@PathVariable String groupId, @PathVariable String userId) {
        chatGroupService.removeGroupMember(groupId, userId);
    }
    
    /**
     * 获取群成员列表
     */
    @GetMapping("/group/{groupId}/members")
    public Set<String> getGroupMembers(@PathVariable String groupId) {
        return chatGroupService.getGroupMembers(groupId);
    }
    
    /**
     * 解散群组
     */
    @DeleteMapping("/group/{groupId}")
    public void dissolveGroup(@PathVariable String groupId) {
        chatGroupService.dissolveGroup(groupId);
    }
    
    /**
     * 发送私聊消息
     */
    @PostMapping("/message/private")
    public void sendPrivateMessage(@RequestParam String fromUserId,
                                 @RequestParam String toUserId,
                                 @RequestParam String content) {
        chatMessageService.sendPrivateMessage(fromUserId, toUserId, content);
    }
    
    /**
     * 发送群聊消息
     */
    @PostMapping("/message/group")
    public void sendGroupMessage(@RequestParam String fromUserId,
                               @RequestParam String groupId,
                               @RequestParam String content) {
        chatMessageService.sendGroupMessage(fromUserId, groupId, content);
    }
    
    /**
     * 标记消息为已读
     */
    @PutMapping("/message/{messageId}/read")
    public void markMessageAsRead(@PathVariable String messageId) {
        chatMessageService.markMessageAsRead(messageId);
    }
    
    /**
     * 获取未读消息数
     */
    @GetMapping("/message/unread/{userId}")
    public long getUnreadMessageCount(@PathVariable String userId) {
        return chatMessageService.getUnreadMessageCount(userId);
    }
} 